package com.example.demo1.until;

import java.sql.*;

public class GetConnection {
    //设置静态的不可更改的私有化变量
    private static final String Url = "jdbc:mysql://localhost:3306/tmxk?serverTimezone=Asia/Shanghai";
    private static final String Username = "root";
    private static final String Password = "123456";
    static Connection conn ;
    static Statement statement;

    {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public GetConnection() {
    }

    public static Connection getConn(){
        Connection  conn = null;
        try {
            conn = DriverManager.getConnection(Url, Username, Password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    /*释放资源 在做查询的时候*/
    public static void close(ResultSet resultSet, Statement statement, Connection conn) {

        try {
            resultSet.close();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /*释放资源 在做更新的时候*/
    public static void close(Statement statement, Connection conn) {

        try {
            statement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
